<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/30
 * Time: 11:41
 */

class Express
{
    private  $AppKey='wPVEuXqC7234';    //秘钥 对应参数id
    private  $url ='http://www.kuaidi100.com/poll';
    private  $schema = 'json';
    private  $callbackurl = 'http://gongying.zxkj918.com/callback/express';
    private  $message = '';

    public function send($company, $number, $from, $to)
    {
        header("Content-Type:text/html; charset=utf-8");
        $data = array(
            'schema' => $this->schema,
            'param'  => json_encode(
                array(
                    'company'    => $company,
                    'number'     => $number,
                    'to'         => $to,
                    'from'       => $from,
                    'key'        => $this->AppKey,
                    'parameters' => json_encode(array('callbackurl'=>$this->callbackurl))
                )
            )
        );

        $data['schema'] = $this->schema;
        $data["param"] = "{'company':'$company', 'number':'$number','from':'$from', 'to':'$to', 'key':'$this->AppKey', 'parameters':{'callbackurl':'$this->callbackurl'}}";
        $o="";
        foreach ($data as $k=>$v)
        {
            $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }
        $data = substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_URL,$this->url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        $result = json_decode($result);
        $this->message = $result->message;
        return $result->result;
    }

    public function getMessage()
    {
        return $this->message;
    }
}